package cn.lf.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.Snowflake;
import cn.hutool.core.util.ObjectUtil;
import cn.lf.dto.shop.ShopReqDTO;
import cn.lf.dto.shop.ShopRespDTO;
import cn.lf.entity.ShopInfo;
import cn.lf.entity.SysUser;
import cn.lf.mapper.ShopInfoMapper;
import cn.lf.mapper.SysUserMapper;
import cn.lf.service.ShopInfoService;
import cn.lf.utils.SnowFlakeUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 店铺信息表(ShopInfo)表服务实现类
 *
 * @author makejava
 * @since 2024-04-07 21:57:55
 */
@Service
public class ShopInfoServiceImpl implements ShopInfoService {

    @Resource
    private ShopInfoMapper shopInfoMapper;

    @Resource
    private SysUserMapper sysUserMapper;

    Snowflake snowflake;

    /**
     * 数据状态--停用
     */
    public static final String DATA_STATUS_DISABLED = "-1";


    /**
     * 数据状态--启用
     */
    public static final String DATA_STATUS_AVAILABLE = "1";


    /**
     * 通过ID查询单条数据
     *
     * @param shopId 主键
     * @return 实例对象
     */
    @Override
    public ShopRespDTO queryById(String shopId) {
        ShopRespDTO respDTO = new ShopRespDTO();
        ShopInfo shopInfo = shopInfoMapper.queryById(shopId);
        SysUser user = sysUserMapper.selectById(shopInfo.getOperator());
        if (ObjectUtil.isNotEmpty(shopInfo)) {
            BeanUtil.copyProperties(shopInfo, respDTO);
            if (Objects.nonNull(user)){
                respDTO.setOperator(user.getFullName());
            }
        }
        return respDTO;
    }

    /**
     * 分页查询
     *
     * @param reqDTO 筛选条件
     * @return 查询结果
     */
    @Override
    public PageInfo queryByPage(ShopReqDTO reqDTO) {
        ShopInfo shopInfo = new ShopInfo();
        //属性复制
        BeanUtil.copyProperties(reqDTO, shopInfo);
        //开启分页
        PageHelper.startPage(reqDTO.getPageNum(), reqDTO.getPageSize());
        //商品列表
        List<ShopInfo> shopInfos = shopInfoMapper.queryAll(shopInfo);
        PageInfo pageInfo = new PageInfo(shopInfos);
        //结果列表
        List<ShopRespDTO> resultList = new ArrayList<>();
        shopInfos.stream().forEach(item -> {
            SysUser user = sysUserMapper.selectById(item.getOperator());
            ShopRespDTO respDTO = new ShopRespDTO();
            BeanUtil.copyProperties(item, respDTO);
            respDTO.setOperator(user.getFullName());
            resultList.add(respDTO);
        });
        pageInfo.setList(resultList);
        return pageInfo;
    }

    /**
     * 新增或更新数据
     *
     * @param reqDTO 实例对象
     * @return 实例对象
     */
    @Override
    public Integer addOrUpdate(ShopReqDTO reqDTO) {
        Integer flag;
        ShopInfo shopInfo = new ShopInfo();
        //新增
        if (ObjectUtil.isEmpty(reqDTO.getShopId())) {
            BeanUtil.copyProperties(reqDTO, shopInfo);
            shopInfo.setShopId(String.valueOf(new SnowFlakeUtil().generatorId()));
            flag = this.shopInfoMapper.insert(shopInfo);
        } else {
            //更新
            BeanUtil.copyProperties(reqDTO, shopInfo);
            flag = this.shopInfoMapper.update(shopInfo);
        }
        return flag;
    }

    /**
     * 通过主键删除数据
     *
     * @param shopId 主键
     * @return 是否成功
     */
    @Override
    public Integer deleteById(String shopId) {
        ShopInfo shopInfo = new ShopInfo();
        shopInfo.setShopId(shopId);
        shopInfo.setStatus(DATA_STATUS_DISABLED);
        return this.shopInfoMapper.update(shopInfo);
    }
}
